package com.wanda.service.impl;

import com.wanda.entity.Screen;
import com.wanda.entity.Site;
import com.wanda.entity.SiteExample;
import com.wanda.entity.Ticket;
import com.wanda.mapper.ScreenMapper;
import com.wanda.mapper.SiteMapper;
import com.wanda.mapper.TicketMapper;
import com.wanda.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author sky
 * @date 2022-08-26
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private ScreenMapper screenMapper;
    @Autowired
    private SiteMapper siteMapper;
    @Autowired
    private TicketMapper ticketMapper;
    /**
     * 插入场次
     * @param screen
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertScreen(Screen screen) {
        int i = screenMapper.insert(screen);
        return i;
    }

    /**
     * 生成座位表
     * @param screenId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void CreateSeat(Long screenId) {
        Screen screen = screenMapper.selectByPrimaryKey(screenId);
        Long hallId = screen.getHallId();
        for (int row = 1; row <= 8; row++) {
            for (int column = 1; column <= 8; column++) {
                if (column == 8){
                    siteMapper.insertSelective(Site.builder().row(new Long(row)).colspan(new Long(column)).isExline((byte) 1).hallId(hallId).isHidden((byte) 0).build());
                }else {
                    siteMapper.insertSelective(Site.builder().row(new Long(row)).colspan(new Long(column)).isExline((byte) 0).hallId(hallId).isHidden((byte) 0).build());
                }
            }
        }
    }

    /**
     * 获取座位表
     */
    @Override
    public List<Site> selectSeat(Long screenId){
        Screen screen = screenMapper.selectByPrimaryKey(screenId);
        Long hallId = screen.getHallId();
//        Long cinemaId = screen.getCinemaId();
        SiteExample siteExample = new SiteExample();
        siteExample.createCriteria().andHallIdEqualTo(hallId);
        List<Site> sites = siteMapper.selectByExample(siteExample);
        return sites;
    }
    /**
     * 生成电影票,返回总票数
     * @param screenId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer CreateTicket(Long screenId) {
        Screen screen = screenMapper.selectByPrimaryKey(screenId);
        Long hallId = screen.getHallId();
        SiteExample siteExample = new SiteExample();
        siteExample.createCriteria().andHallIdEqualTo(hallId);
        List<Site> sites = siteMapper.selectByExample(siteExample);
        for (Site site : sites) {
            ticketMapper.insertSelective(Ticket.builder().screenId(screenId).siteId(site.getId()).isWicket((byte) 0).version(new Long(0)).build());
        }
        return sites.size();
    }

}
